#include "../import/head.h"
#include "get_zookeeper.h"
#include "../data_struct/head.h"
#include "../global/head.h"
#include "../macro/head.h"
#include "../msg/head.h"
#include "os_init.h"

///////////////////////////////////////////////////////////////////////////////////////////////
// 

namespace xos_sgsj
{
    get_zookeeper::get_zookeeper()
    {
        m_pZookeeperMgr = 0;
        m_pZookeeper = 0;
    }

    get_zookeeper::~get_zookeeper()
    {
        term();
    }

    ///////////////////////////////////////////////////////////////////////////////////////////////
    // 

    int get_zookeeper::run_task( xos::i_msg * pMsg )
    {
        int ret = 0;

        data * p = g_pSysData->m_pPool->m_data_pool.GetItem();
        p->init();
        p->m_nMsg = MSG_ZOOKEEPER;
        p->m_pMsg = pMsg;
        g_pSysData->m_pImpl->notify( p );
        p = 0;

        return ret;
    }

    int get_zookeeper::init()
    {
        int ret = 0;

        if( 0 == ret )
        {
            xos_zookeeper::create( g_pSysData->m_os.m_pXos, &m_pZookeeperMgr );
            if( !m_pZookeeperMgr )
            {
                ret = 1;
            }
        }

        if( 0 == ret )
        {
            ret = m_pZookeeperMgr->create( &m_pZookeeper );
        }

        if( 0 == ret )
        {
            ret = m_pZookeeperMgr->set_callback_thread( g_pSysData->m_os.m_pMiscCallbackThread );
            m_pZookeeper->set_callback( this );
        }

        if( 0 == ret )
        {
            g_pSysData->m_os.m_pZookeeperMgr = m_pZookeeperMgr;
            g_pSysData->m_os.m_pZookeeper = m_pZookeeper;
        }

        return ret;
    }

    int get_zookeeper::term()
    {
        int ret = 0;

        g_pSysData->m_os.m_pZookeeperMgr = 0;
        g_pSysData->m_os.m_pZookeeper = 0;

        if( m_pZookeeper )
        {
            m_pZookeeper->release();
            m_pZookeeper = 0;
        }

        if( m_pZookeeperMgr )
        {
            m_pZookeeperMgr->release();
            m_pZookeeperMgr = 0;
        }

        return ret;
    }

} // xos_sgsj
